package com.yanqu.road.server.timer.client.msg;

import com.yanqu.road.entity.enums.client.msg.push.eClickType;
import com.yanqu.road.entity.enums.client.msg.push.ePushType;
import com.yanqu.road.entity.enums.client.msg.push.eTargetType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.push.ClientPushRecord;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.logic.thread.client.msg.push.task.PushClientMsgTask;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.sdk.ClientMsgPushMgr;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wpj
 * @Date 2021/4/11
 */
public class ClientPushMsgTask extends GeneralTask {

    static int ONE_DAY = 24 * 60 * 60;
    static int ONE_HOUR = 60 * 60;
    static int ONE_MINUTE = 60;

    @Override
    public void doRun() {
        String[] strings = ChannelConfig.OPEN_CLIENT_MSG_PUSH.split(";");
        if (strings[1].equals("0")) {
            // 关闭 不判断
            return;
        }
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000;
        long nowTime = System.currentTimeMillis() / 1000;

        if (nowTime <= zeroTime + Integer.valueOf(strings[0].split("-")[0]) * ONE_HOUR
                || nowTime >= zeroTime + Integer.valueOf(strings[0].split("-")[1])  * ONE_HOUR) {
            // 不在规定时间范围内 不推送
            return;
        }

        // appId childGameId type
        Map<String, Map<String, Map<Integer, List<Long>>>> actListMap = new HashMap<>();
        for (UserInfo userInfo : UserMgr.getUserInfoList()) {

            if (StringUtils.isNullOrEmpty(userInfo.getAppId()) || StringUtils.isNullOrEmpty(userInfo.getChildGameId())) {
                // appId + childGameId为空
                continue;
            }

            if (userInfo.getLastLoginTime() == 0) {
                // 新注册账号
                continue;
            }


            long dValue = nowTime - userInfo.getLastLoginTime();

            if (userInfo.getTitleId() >= 3 && dValue < 3 * ONE_DAY) {
                // 身份等级大于等3 并且 离线时间在3天内 // 暂时不退送这两个
//
//                if (nowTime - userInfo.getLastLoginTime() >= (7 * ONE_HOUR + 30 * ONE_MINUTE)) {
//                    // 离线超过7.5小时的推送传唤
//                    ClientPushRecord clientPushRecord = ClientMsgPushMgr.getClientPushRecord(Long.valueOf(userInfo.getPlatformUserId()), ePushType.BEAUTY_TRYST.getSchoolType());
//                    if (clientPushRecord.getTimes() == 0) {
//                        addPlayerId(actListMap, userInfo.getAppId(), userInfo.getChildGameId(), ePushType.BEAUTY_TRYST.getSchoolType(), Long.valueOf(userInfo.getPlatformUserId()));
//                    }
//                }
//
//                if (nowTime - userInfo.getLastLoginTime() >= (10 * ONE_HOUR)) {
//                    // 离线超过10小时推送活力培养
//                    ClientPushRecord clientPushRecord = ClientMsgPushMgr.getClientPushRecord(Long.valueOf(userInfo.getPlatformUserId()), ePushType.CHILD_TRAIN.getSchoolType());
//                    if (clientPushRecord.getTimes() == 0) {
//                        addPlayerId(actListMap, userInfo.getAppId(), userInfo.getChildGameId(), ePushType.CHILD_TRAIN.getSchoolType(), Long.valueOf(userInfo.getPlatformUserId()));
//                    }
//                }
            }

            if (dValue <= 7 * ONE_DAY) {
                // 离线时间7天内
                if (userInfo.getTitleId() >= 12 && nowTime >= zeroTime + (55 * ONE_MINUTE + 11 * ONE_HOUR) && nowTime <= zeroTime + 12 * ONE_HOUR) {
                    // 等级大于等于12级 11点到12点 推送狩猎
                    ClientPushRecord clientPushRecord = ClientMsgPushMgr.getClientPushRecord(Long.valueOf(userInfo.getPlatformUserId()), ePushType.BOSS_12.getType());
                    if (clientPushRecord.getTimes() == 0) {
                        addPlayerId(actListMap, userInfo.getAppId(), userInfo.getChildGameId(), ePushType.BOSS_12.getType(), Long.valueOf(userInfo.getPlatformUserId()));
                    }
                }

                if (userInfo.getTitleId() >= 20 && nowTime >= zeroTime + (55 * ONE_MINUTE + 19 * ONE_HOUR) && nowTime <= zeroTime + 20 * ONE_HOUR) {
                    // 等级大于等于20级 19点到20点 推送青龙
                    ClientPushRecord clientPushRecord = ClientMsgPushMgr.getClientPushRecord(Long.valueOf(userInfo.getPlatformUserId()), ePushType.BOSS_20.getType());
                    if (clientPushRecord.getTimes() == 0) {
                        addPlayerId(actListMap, userInfo.getAppId(), userInfo.getChildGameId(), ePushType.BOSS_20.getType(), Long.valueOf(userInfo.getPlatformUserId()));
                    }
                }
            }
        }

        for (String appId : actListMap.keySet()) {
            for (String childGameId : actListMap.get(appId).keySet()) {
                for (int pushType : actListMap.get(appId).get(childGameId).keySet()) {
                    ClientMsgPushMgr.addClientMsgPushTask(new PushClientMsgTask(ClientMsgPushMgr.getClientPushContent(pushType).getTitle(), ClientMsgPushMgr.getClientPushContent(pushType).getMsg(), eTargetType.LIST.getName(), StringUtils.listToString(actListMap.get(appId).get(childGameId).get(pushType), ","), eClickType.START_APP.getName(), "", "", appId, childGameId, pushType));
                }
            }
        }

    }

    public void addPlayerId(Map<String, Map<String, Map<Integer, List<Long>>>> map, String appId, String childGameId, int type, long userId) {
        if (!map.containsKey(appId)) {
            map.put(appId, new HashMap<>());
        }

        if (!map.get(appId).containsKey(childGameId)) {
            map.get(appId).put(childGameId, new HashMap<>());
        }

        if (!map.get(appId).get(childGameId).containsKey(type)) {
            map.get(appId).get(childGameId).put(type, new ArrayList<>());
        }

        if (map.get(appId).get(childGameId).get(type).size() >= 200) {
            // 超过200人走下一次逻辑
            return;
        }
        map.get(appId).get(childGameId).get(type).add(userId);
    }



}
